(function () {
    console.log("预警信息文件");
    let now_open_diag = {}
    now_open_diag.API_URL = ' '
    //创建一个三维场景
    document.getElementById("todayTimer").innerHTML = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
    const devicePixelRatio = window.devicePixelRatio;//设备分辨率
    function Request(baseUrl = '', time = 1000) {
        this.baseUrl = baseUrl;

        function request(url, option = {}) {
            return new Promise(async (resolve, reject) => {
                try {
                    const response = await fetch(baseUrl + url, option);
                    if (response.status == '200') {
                        const data = await response.json();
                        if (data.code == '200') {
                            resolve(data);
                        } else {
                            reject(data.message || '请求失败');
                        }
                    } else {
                        reject(response);
                    }
                } catch (e) {
                    reject(e);
                }
            })
        }

        this.post = function (url, data) {
            return request(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            })
        }

        this.get = function (url) {
            return request(url, {
                method: 'GET'
            })
        }
    }


    Request.createRequest = function (baseUrl) {
        return new Request(baseUrl);
    }
    const req = Request.createRequest(now_open_diag.API_URL);
    let rsqState = [0, 0, 0, 0] //燃烧器状态  1是停磨
    let state = null
    function getrunstate() {
        clearInterval(state)
        req.post('/spf/visit/getThermalFieldAll').then(res => {
            rsqState[0] = res.data.runStateA
            rsqState[1] = res.data.runStateB
            rsqState[2] = res.data.runStateC
            rsqState[3] = res.data.runStateD
            state = setInterval(() => { getrunstate() }, 1000)
        })
    }
    let initshishi = null
    const initDataFromINnput = () => {
        removeLoader('加载中....')
        clearInterval(initshishi)
        //重置数据
        // 获取输入框值
        req.get('/api/sjdl/home/point/value').then(res => {
            let tablenumber = []
            Array(tablenumber)
            let tablebox = []
            let oIndexDiv = document.getElementsByClassName("yj_item")
            // let pointArr = ["A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4", "C1", "C2", "C3", "C4", "D1", "D2", "D3", "D4", "E1", "E2", "E3", "E4", "F1", "F2", "F3", "F4"]
            let pointArr = ["A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4", "C1", "C2", "C3", "C4", "D1", "D2", "D3", "D4"]



            for (let key in res.data) {
                tablebox.push({
                    label: res.data[key], // label 字段
                    value: key // value字段
                })
            }
            tablebox.sort(function (a, b) {
                return (a.value + '').localeCompare(b.value + '')
            })
            for (let i = 0; i < tablebox.length; i++) {
                tablenumber.push({
                    nameber: tablebox[i].label.TR.toFixed(0),
                    value: tablebox[i].label.warnData
                })
            }
            for (let i = 0; i < tablebox.length; i++) {
                tablenumber.push({
                    nameber: tablebox[i].label.ZHJL.toFixed(0),
                    value: tablebox[i].label.warnData
                })
            }
            for (let i = 0; i < tablebox.length; i++) {
                tablenumber.push({
                    nameber: tablebox[i].label.RSWDX.toFixed(0),
                    value: tablebox[i].label.warnData
                })

            }
            for (let index = 0; index < 3; index++) {
                // console.log(index);
                let str = ""
                str += '<h1>' + (index == 0 ? '温度' : index == 1 ? '着火距离' : index == 2 ? '燃烧稳定性' : "") + '</h1>'
                for (let i = 0; i < 16; i++) {
                    let irsqState = i < 4 ? 0 : i > 3 && i < 8 ? 1 : i > 7 && i < 12 ? 2 : i > 11 && i < 16 ? 3 : 4

                    if (i % 4 == 0) {
                        str += "<p>"
                    }
                    let startId = index * sessionStorage.getItem('glnumber') * 4
                    // let nowValue = Number(document.getElementById(startId + 4 + "").value).toFixed(0)
                    let nowValue = Number(tablenumber[startId + i].nameber)
                    let level_1 = Number(tablenumber[startId + i].value.tempLowLower)
                    let level_2 = Number(tablenumber[startId + i].value.tempLower)
                    let level_3 = Number(tablenumber[startId + i].value.tempUpUpper)
                    let level_4 = Number(tablenumber[startId + i].value.tempUpper)
                    let level_5 = Number(tablenumber[startId + i].value.distinceLowLower)
                    let level_6 = Number(tablenumber[startId + i].value.distinceLower)
                    let level_7 = Number(tablenumber[startId + i].value.distinceUpUpper)
                    let level_8 = Number(tablenumber[startId + i].value.distinceUpper)
                    let level_9 = Number(tablenumber[startId + i].value.stabilityLowLower)
                    let level_10 = Number(tablenumber[startId + i].value.stabilityLower)
                    if (rsqState[irsqState] == 1) { //停磨
                        if (index == 0 && Number(nowValue) > 1450) { //温度大于1450 标红
                            str += '<span style="color:red;width:100px">' + pointArr[i] + '</span>'
                        } else if (index > 0) {
                            // str += '<span style="color:#009100;width:100px">' + '0' + '</span>'
                            str += '<span style="color:#009100;width:100px">' + pointArr[i] + '</span>'

                        } else {
                            str += '<span style="color:#009100;width:100px">' + pointArr[i] + '</span>'
                        }

                    } else {
                        if (index == 0) {
                            if (level_3 <= nowValue || nowValue <= level_1) {
                                str += '<span style="color:red;width:100px">' + pointArr[i] + '</span>'
                            } else if (level_1 <= nowValue && nowValue <= level_4) {
                                str += '<span style="color:#009100;width:100px">' + pointArr[i] + '</span>'
                            } else if (level_1 <= nowValue && nowValue <= level_2 || level_4 <= nowValue && nowValue <= level_3) {
                                str += '<span style="color:yellow;width:100px">' + pointArr[i] + '</span>'
                            }
                        } else if (index == 1) {
                            if (nowValue == 0) {
                                str += '<span style="color:#009100;width:100px">' + pointArr[i] + '</span>'
                            }
                            else if (level_7 <= nowValue || nowValue <= level_5) {
                                str += '<span style="color:red;width:100px">' + pointArr[i] + '</span>'
                            } else if (level_5 <= nowValue && nowValue <= level_8) {
                                str += '<span style="color:#009100;width:100px">' + pointArr[i] + '</span>'
                            } else if (level_5 <= nowValue && nowValue <= level_6 || level_8 <= nowValue && nowValue <= level_7) {
                                str += '<span style="color:yellow;width:100px">' + pointArr[i] + '</span>'
                            }
                        } else {
                            if (nowValue == 0) {
                                str += '<span style="color:#009100;width:100px">' + pointArr[i] + '</span>'
                            }
                            else if (nowValue <= level_9) {
                                str += '<span style="color:red;width:100px">' + pointArr[i] + '</span>'
                            } else if (nowValue >= level_10) {
                                str += '<span style="color:#009100;width:100px">' + pointArr[i] + '</span>'
                            } else if (level_9 <= nowValue && nowValue <= level_10) {
                                str += '<span style="color:yellow;width:100px">' + pointArr[i] + '</span>'
                            }
                        }
                    }


                    if ((i + 1) % 4 == 0) {
                        str += "</p>"
                    }
                }
                oIndexDiv[index].innerHTML = str
            }
            initshishi = setInterval(() => { initDataFromINnput() }, 1000)
        })
    }

    const initEvent = (str) => {//
        //获取事件元素
        let oFootBtns = document.getElementById(str).children //底部菜单 btns
        if (str == "zbf_foot_btns") {
            now_open_diag.username == "admin" ? "" : oFootBtns[oFootBtns.length - 1].style.display = "none"
            // now_open_diag.username == "admin" ? "" : oFootBtns[oFootBtns.length - 2].style.display = "none"
        }
        for (let i = 0; i < oFootBtns.length; i++) {
            oFootBtns[i].onclick = () => {
                for (let ii = 0; ii < oFootBtns.length; ii++) {
                    oFootBtns[ii].className = ""
                }
                oFootBtns[i].className = "active"
                if (str == "zbf_r_btns") {
                    if (i == 8) {
                        for (let o = 6; o < 14; o++) {
                            scene.children[2].children[o].visible = false
                        }
                    } else {
                        scene.children[2].children[13 - i].visible = !scene.children[2].children[13 - i].visible
                    }
                } else {
                    function closeDiag() {
                        timer ? clearInterval(timer) : ""
                        oTodayTimer ? clearInterval(oTodayTimer) : ""

                    }
                    switch (i) {
                        case 0:
                            closeDiag()
                            setTimeout(() => {
                                window.location.href = "/index.html"
                            }, 200)
                            break;
                        case 1:
                            closeDiag()
                            setTimeout(() => {
                                // openFM("全炉膛温度场", 11, "SJDL_IFS_DATA.prj", "全炉膛温度场")
                                window.location.href = "/quanlutang.html"
                            }, 200)

                            break;
                        case 2:
                            closeDiag()
                            setTimeout(() => {
                                // openFM("横截面", 6, "SJDL_IFS_DATA.prj", "横截面")
                                window.location.href = "/hjm.html"
                            }, 200)

                            break;
                        case 3:
                            closeDiag()
                            setTimeout(() => {
                                // openFM("燃烧稳定性监测", 7, "SJDL_IFS_DATA.prj", "燃烧稳定性监测")
                                window.location.href = "/ranshaowendingxing.html"
                            }, 200)

                            break;
                        case 4:
                            closeDiag()
                            setTimeout(() => {
                                // openFM("煤粉分配率", 9, "SJDL_IFS_DATA.prj", "煤粉分配率")
                                window.location.href = "/meifen.html"
                            }, 200)

                            break;
                        case 5:
                            closeDiag()
                            setTimeout(() => {
                                // openFM("煤粉颗粒度分布", 8, "SJDL_IFS_DATA.prj", "煤粉颗粒度分布")
                                window.location.href = "/main/dcimgs/gl/main.html"
                            }, 200)

                            break;
                        case 6:
                            closeDiag()
                            setTimeout(() => {
                                // openFM("预警信息", 12, "SJDL_IFS_DATA.prj", "预警信息")
                                window.location.href = "/yujing.html"
                            }, 200)

                            break;
                        case 7:
                            closeDiag()
                            setTimeout(() => {
                                // openFM("历史数据", 10, "SJDL_IFS_DATA.prj", "历史数据")
                                window.location.href = "/history.html"
                            }, 200)

                            break;
                        case 8:
                            closeDiag()
                            setTimeout(() => {
                                // openFM("历史数据", 10, "SJDL_IFS_DATA.prj", "历史数据")
                                window.location.href = "/dcsHistory.html"
                            }, 200)
                            break;
                        case 9:
                            closeDiag()
                            setTimeout(() => {
                                // openFM("预警阈值配置修改", 14, "SJDL_IFS_DATA.prj", "预警阈值配置修改")
                                window.location.href = "/canshushezhi.html"
                            }, 200)

                            break;
                        default:

                    }

                }
            }
        }
    }

    initEvent('zbf_foot_btns') //底部菜单 btns
    // let c_form = eval('formview_' + now_open_diag.fmname)
    // c_form.onready = function () {// 页面打开 数据加载完成
    //     initDataFromINnput()
    // }
    lsLoader('加载中....')
    let timer = setInterval(() => {
        initDataFromINnput()
        getrunstate()
        clearInterval(timer)
    }, 1000)
    let oTodayTimer = setInterval(() => {
        document.getElementById("todayTimer").innerHTML = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
    }, 1000)


})()
